#ifndef WXSITEMFACTORY_H
#define WXSITEMFACTORY_H

#include "wxsiteminfo.h"
#include "../wxsautoresourcetreeimage.h"
#include "../wxscodinglang.h"
#include <wx/string.h>
#include <wx/hashmap.h>
#include <wx/imaglist.h>
#include <configmanager.h>

#if defined(__WXMSW__) && defined(LoadImage)
#undef LoadImage
#endif


class wxsItem;
class wxsItemResData;

class wxsItemFactory {
  public:
    static wxsItem* Build( const wxString& ClassName, wxsItemResData* Data );
    static const wxsItemInfo* GetInfo( const wxString& ClassName );
    static const wxsItemInfo* GetFirstInfo();
    static const wxsItemInfo* GetNextInfo();
    static wxImageList& GetImageList();
    static int LoadImage( const wxString& Name );
    wxsItemFactory( const wxsItemInfo* Info );
    wxsItemFactory( const wxsItemInfo* Info, wxString ClassName );
    virtual ~wxsItemFactory();
  protected:
    virtual wxsItem* OnBuild( wxsItemResData* Data ) = 0;
  private:
    WX_DECLARE_STRING_HASH_MAP( wxsItemFactory*, ItemMapT );
    static ItemMapT& ItemMap();
    const wxsItemInfo* m_Info;
    wxString m_Name;
    static ItemMapT::iterator m_Iter;
};

template<class T> class wxsRegisterItem: public wxsItemFactory {
  public:
    wxsItemInfo Info;
    wxsRegisterItem( wxString ClassNameWithoutWx,wxString name, wxsItemType Type, wxString Category, long Priority, bool AllowInXRC = true ):
      wxsItemFactory( &Info, _T( "wx" ) + ClassNameWithoutWx ),
      m_TreeImage( _T( "images/wxsmith/wx" ) + ClassNameWithoutWx + _T( ".png" ), true ) {
      Info.ClassName = _T( "wx" ) + ClassNameWithoutWx;
      Info.Type = Type;
      Info.name = name;
      Info.Category =Category;  //控件类别
      Info.Priority =Priority;
      Info.DefaultVarName = ClassNameWithoutWx;
      Info.AllowInXRC = AllowInXRC;
      wxString DataPath = ConfigManager::GetDataFolder() + _T( "/images/wxsmith/" );
      Info.Icon.LoadFile( DataPath + Info.ClassName + _T( ".png" ), wxBITMAP_TYPE_PNG );
      Info.TreeIconId = m_TreeImage.GetIndex();
    }
  protected:
    virtual wxsItem* OnBuild( wxsItemResData* Data ) {
      return new T( Data );
    }
    wxsAutoResourceTreeImage m_TreeImage;
};

#endif
